#include "stdafx.h"
#include "MultiObjectNode.h"

#include "GfxObject.h"
#include "Ray.h"
#include "Scene.h"
#include "RenderingSystem.h"

using namespace mscene;

MultiObjectNode::MultiObjectNode(Scene & scene, const char * name)
	: Node(scene, name)
{
	Type = MULTIOBJECT;
}

MultiObjectNode::MultiObjectNode( const MultiObjectNode & src )
	: Node(src)
{
	for(uint i = 0; i < src.GfxObjects.size(); i++)
		addGfxObject(*src.GfxObjects[i]->copy());
}

MultiObjectNode::~MultiObjectNode()
{
	for(uint i = 0; i < GfxObjects.size(); i++)
		mScene.RenderSys.removeGfxObject(*GfxObjects[i]);
}

MultiObjectNode* MultiObjectNode::copy()
{
	return new MultiObjectNode(*this);
}

void MultiObjectNode::update( float dt )
{
	Node::update(dt);

	for(uint i = 0; i < GfxObjects.size(); i++)
	{
		GfxObjects[i] = &mScene.RenderSys.findGfxObject(GfxObjectsIDs[i]);
		GfxObjects[i]->updateDrawData(Position, Orientation, Scale);
	}
}

float MultiObjectNode::rayIntersection(const mmath::Ray & ray)
{
	return ray.intersectSphere(XMLoadFloat3(&Position), 5);
}

void MultiObjectNode::addGfxObject(mgfx::GfxObject & object)
{
	GfxObjects.push_back(&object);
	GfxObjectsIDs.push_back(object.Id);
}